<?php

namespace App\Repos;

use App\Models\Question;
use App\Models\User;
use DB;

class QuestionRepo 
{
    // 我的问答页面使用
    public static function getPages($where=[],$num=10)
    {
    	$list  = DB::table('questions as i')
					->select(['i.id','u.nickname','u.avatar','i.created_at','i.to_user_id','i.from_user_id','i.status','i.msg','i.feedback','i.is_read','u.sex',DB::raw('left(u.truename, 1) as firstname')])
					->leftJoin('users as u','u.id','=','i.from_user_id')
					->where($where)
                    ->orderBy('i.is_read','asc')
                    ->orderBy('i.created_at','desc')
					->paginate($num);
    	
    	return $list;
    }

    public static function save($data)
    {
        $rs['status'] = true;
        $hasWhere = array_only($data,['from_user_id','to_user_id']);
        $hasData = Question::where($hasWhere)->first();
        if($hasData) {
            $rs['status'] = false;
            $rs['msg'] = '只能提问一次哦';
            return $rs;
        }

    	$ret = Question::create($data);
        $rs['data'] = $ret;
    	return $rs;
    }

    // 单挑信息问我的
    public static function find($where=[])
    {
        $data = DB::table('questions as i')
                    ->select(['i.id','u.nickname','u.avatar','i.created_at','i.to_user_id','i.from_user_id','i.status','i.msg'])
                    ->leftJoin('users as u','u.id','=','i.from_user_id')
                    ->where($where)
                    ->first();
        return $data;
    }



    public static function feedback($where=[],$data)
    {
        return Question::where($where)->update($data);
    }

    public static function update($where,$data)
    {
        return Question::where($where)->update($data);
    }

    
}
